package com.ruoyi.combat.service.apprisk.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.combat.domain.apprisk.CombatAppRisk;
import com.ruoyi.combat.domain.apprisk.CombatApplication;
import com.ruoyi.combat.domain.apprisk.CombatPhone;
import com.ruoyi.combat.mapper.apprisk.CombatAppRiskMapper;
import com.ruoyi.combat.mapper.apprisk.CombatApplicationDataMapper;
import com.ruoyi.combat.service.apprisk.CombatApplicationDataService;
import com.ruoyi.combat.service.apprisk.ICombatApplicationService;
import com.ruoyi.combat.service.apprisk.ICombatPhoneService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CombatApplicationDataServiceImpl implements CombatApplicationDataService {

    @Autowired
    public ICombatPhoneService combatPhoneService;
    @Autowired
    private ICombatApplicationService combatApplicationService;
    @Autowired
    private CombatApplicationDataMapper combatApplicationDataMapper;

    @Autowired
    private CombatAppRiskMapper combatAppRiskMapper;

    @Override
    public Map<String, Long> getNum() {
        Map<String, Long> result = new HashMap<>();
        CombatPhone combatPhone = new CombatPhone();
        List<CombatPhone> combatPhones = combatApplicationDataMapper.selectCombatPhoneList(combatPhone);
        CombatApplication combatApplication = new CombatApplication();
        List<CombatApplication> combatApplications = combatApplicationDataMapper.selectCombatApplicationList(combatApplication);
        List<CombatAppRisk> combatAppRisks = combatApplicationDataMapper.selectCombatAppRiskList(new CombatAppRisk());
        result.put("terminalNum", (long) combatPhones.size());
        result.put("apkNum", (long) combatApplications.size());
        result.put("riskNum", (long) combatAppRisks.size());
        DateTime dateTime = DateUtil.beginOfDay(new Date());
        String format = DateUtil.format(dateTime, "yyyy-MM-dd HH:mm:ss");
        combatPhone.setPhoneFirstAccess(format);
        List<CombatPhone> combatPhones1 = combatApplicationDataMapper.selectCombatPhoneList(combatPhone);
        combatApplication.setCreateTime(dateTime);
        List<CombatApplication> combatApplications1 = combatApplicationDataMapper.selectCombatApplicationList(combatApplication);
        result.put("terminalAddNum", (long) combatPhones1.size());
        result.put("appAddNum", (long) combatApplications1.size());


        return result;
    }

    @Override
    public List<Map<String, String>> getAddNumByDay() {
        List<Map<String,String>> maps = combatApplicationDataMapper.gePhoneCountByday();
        List<Map<String,String>> appCountByday = combatApplicationDataMapper.getAppCountByday();
        List<Map<String, String>> result = new ArrayList<>();
        appCountByday.forEach(map -> {

            String o1 = String.valueOf(map.get("click_date"));
            Map<String, String> data = new HashMap<>();
            data.put("date", o1);
            data.put("apk", String.valueOf(map.get("count")));
            for (Map<String,String> map1 : maps) {
                String o = String.valueOf(map1.get("click_date"));
                if (o .equals(o1)) {
                    data.put("terminal", String.valueOf(map1.get("count")));
                }
            }
            result.add(data);

        });
        return result;
    }

    /**
     * 各类风险占比
     */
    @Override
    public List<Map<String, String>> appRiskCount() {
        List<Map<String, String>> maps = combatAppRiskMapper.appRiskCount();
        return maps;
    }

    @Override
    public List<Map<String, String>> apkRiskRank() {

        return combatApplicationDataMapper.apkRiskRank();
    }

    @Override
    public List<Map<String, String>> terminalRiskRank() {
        return combatApplicationDataMapper.terminalRiskRank();
    }
}
